# encoding: utf-8
"""
Beetle-Agent
"""
import logging
import logging.config
import os

# import socketio
from flask import Flask

from app.modules.apps import App
from app.modules.common.port import ProtectedPortGroupPool
from config import BaseConfig

container_pool = {}

CONFIG_NAME_MAPPER = {
    'development': 'config.DevelopmentConfig',
    'testing': 'config.TestingConfig',
    'production': 'config.ProductionConfig',
    'local': 'local_config.LocalConfig',
}

app = None
def create_app(flask_config='production', **kwargs):
    """
    Entry point to the Ucloud-Agent application.
    """
    global app
    app = Flask(__name__, **kwargs)
    config_name = os.getenv('FLASK_CONFIG', flask_config)
    app.config.from_object(CONFIG_NAME_MAPPER[config_name])

    # if app.debug:
    #     logging.getLogger('flask_oauthlib').setLevel(logging.DEBUG)
    #     app.logger.setLevel(logging.DEBUG)

        # We don't need default Flask's loggers when using invoke tasks as the
        # latter set up colorful loggers.
        # for handler in app.logger.handlers:
        #     app.logger.removeHandler(handler)

    # wrap Flask application with socketio's middleware

    _log_config_file = '%s/logging.conf' % BaseConfig.PROJECT_ROOT

    logging.config.fileConfig(fname = _log_config_file)


    from . import extensions
    extensions.init_app(app)

    from . import modules
    modules.init_app(app)

    return app
